package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*LTAI5tKjF7HS2UHtymv6wyCN
9AQjJTWwdJhdC76L90aFqZdoXwtSQi*/
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    //分类列表
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                 String name){
        Page<Dish> page=dishService.findByPage(pageNum,pageSize,name);
        return ResultInfo.success(page);
    }
    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        dishService.save(dish);
        return ResultInfo.success(null);
    }
    //修改菜品，回显菜品
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }
    //修改菜品
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(null);
    }
    //批量删除
    @DeleteMapping("/dish")
    public ResultInfo delete(@RequestParam List<Long> ids){
        if (CollectionUtil.isEmpty(ids)) {
            throw new CustomException("请勾选要删除的菜品");
        }
        dishService.delete(ids);
        return ResultInfo.success(null);
    }
    //批量启售、禁售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);
        System.out.println(status);
        System.out.println(ids);
        return ResultInfo.success(null);
    }

    //新增套餐菜品分类选择
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(Dish dish){
        List<Dish> list = dishService.findByCategoryId(dish);
        return ResultInfo.success(list);
    }

}
